<?php
namespace app\admin\controller;

use app\common\cache\common\IAreaCache;
use app\common\controller\AdminBaseController;
use app\common\ext\IDb;
use app\common\ext\IRequest;

class UserApply extends AdminBaseController{

    /**
     * @功能：信用卡申请日志
     * @开发者：cxl
     * @return string
     */
    public function credit_apply_list(){
        //接收参数
        $user_id = IRequest::get("user_id");
        $credit_product_id = IRequest::get("credit_product_id");
        $area = IRequest::get("area");

        //整理区域数据
        $province_id = 0;
        $city_id = 0;
        if(!empty($area) && is_array($area)){
            if(!empty($area[0])) $province_id = $area[0]; //取得省份
            if(!empty($area[1])) $city_id = $area[1]; //取得城市
        }

        //取得属性分类列表
        $area_list = IAreaCache::getAllList(2);

        //取得数据
        $where = null;
        if(!empty($user_id)) $where['user_id'] = $user_id;
        if(!empty($credit_product_id)) $where['credit_product_id'] = $credit_product_id;
        if(!empty($province_id)) $where['province_id'] = $province_id;
        if(!empty($city_id)) $where['city_id'] = $city_id;
        $credit_apply_list = IDb::getInstance("credit_apply")
            ->setDbWhere($where)
            ->setDbOrder("credit_apply_time desc")
            ->pag();

        //页面赋值
        $this->assign("user_id",$user_id);
        $this->assign("credit_product_id",$credit_product_id);
        $this->assign("area",$area);
        $this->assign("area_list",$area_list);
        $this->assign("credit_apply_list",$credit_apply_list);

        //渲染模板输出
        return view();
    }

    /**
     * @功能：信用卡申请详情
     * @开发者：cxl
     * @return string
     */
    public function credit_apply_info(){
        //接收参数
        $credit_apply_id = IRequest::get("credit_apply_id",IRequest::NOT_EMPTY,"信用卡申请编号不能为空！");

        //取得日志
        $credit_apply_where['credit_apply_id'] = $credit_apply_id;
        $credit_apply_info = IDb::getInstance('credit_apply')->setDbWhere($credit_apply_where)->row();
        if(empty($credit_apply_info)){
            error("信用卡申请信息失败！");
        }

        //取得相关用户材料数据
//        $user_material_where['user_id'] = $credit_apply_info['user_id'];
//        $user_material_list = IDb::getInstance('user_material')->setDbWhere($user_material_where)->sel();
//        if($user_material_list === false){
//            error("信用卡申请信息失败！");
//        }

        //判断数据是否存在
        if(empty($user_material_list)){
            $user_material_list = array();
        }

        //设置layout
        $this->layout("layout_empty");

        //页面赋值
        $this->assign("credit_apply_id",$credit_apply_id);
        $this->assign("user_material_list",$user_material_list);
        $this->assign("credit_apply_info",$credit_apply_info);

        //渲染模板输出
        return view();
    }

    /**
     * @功能：信用卡申请日志
     * @开发者：cxl
     * @return string
     */
    public function credit_apply_log(){
        //接收参数
        $credit_apply_id = IRequest::get("credit_apply_id",IRequest::NOT_EMPTY,"信用卡编号不能为空！");

        //取得日志
        $where['credit_apply_id'] = $credit_apply_id;
        $credit_apply_log_list = IDb::getInstance('credit_apply_log')->setDbWhere($where)->setDbOrder("credit_apply_log_id desc")->pag();

        //设置layout
        $this->layout("layout_empty");

        //页面赋值
        $this->assign("credit_apply_log_list",$credit_apply_log_list);

        //渲染模板输出
        return view();
    }

    /**
     * @功能：贷款产品申请日志
     * @开发者：cxl
     * @return string
     */
    public function loan_apply_list(){
        //接收参数
        $user_id = IRequest::get("user_id");
        $loan_product_id = IRequest::get("loan_product_id");
        $loan_apply_status = IRequest::get("loan_apply_status");
        $area = IRequest::get("area");

        //整理区域数据
        $province_id = 0;
        $city_id = 0;
        if(!empty($area) && is_array($area)){
            if(!empty($area[0])) $province_id = $area[0]; //取得省份
            if(!empty($area[1])) $city_id = $area[1]; //取得城市
        }

        //取得属性分类列表
        $area_list = IAreaCache::getAllList(2);

        //取得数据
        $where = null;
        if(!empty($user_id)) $where['user_id'] = $user_id;
        if(!empty($loan_product_id)) $where['loan_product_id'] = $loan_product_id;
        if(!empty($loan_apply_status)) $where['loan_apply_status'] = $loan_apply_status;
        if(!empty($province_id)) $where['province_id'] = $province_id;
        if(!empty($city_id)) $where['city_id'] = $city_id;
        $loan_apply_list = IDb::getInstance("loan_apply")
            ->setDbWhere($where)
            ->setDbOrder("loan_apply_time desc")
            ->pag();

        //页面赋值
        $this->assign("user_id",$user_id);
        $this->assign("loan_product_id",$loan_product_id);
        $this->assign("loan_apply_status",$loan_apply_status);
        $this->assign("area",$area);
        $this->assign("area_list",$area_list);
        $this->assign("loan_apply_list",$loan_apply_list);

        //渲染模板输出
        return view();
    }

    /**
     * @功能：贷款产品申请详情
     * @开发者：cxl
     * @return string
     */
    public function loan_apply_info(){
        //接收参数
        $loan_apply_id = IRequest::get("loan_apply_id",IRequest::NOT_EMPTY,"贷款产品申请编号不能为空！");

        //取得日志
        $loan_apply_where['loan_apply_id'] = $loan_apply_id;
        $loan_apply_info = IDb::getInstance('loan_apply')->setDbWhere($loan_apply_where)->row();
        if(empty($loan_apply_info)){
            error("贷款产品申请信息失败！");
        }

        //TODO 取得日志
        /*$loan_apply_material_link_where['loan_apply_id'] = $loan_apply_id;
        $loan_apply_material_link_list = IDb::getInstance('loan_apply_material_link')->setDbWhere($loan_apply_material_link_where)->sel();*/

        //取得申请资料信息
        $user_material_list = array();
        if(!empty($loan_apply_material_link_list) && is_array($loan_apply_material_link_list)){
            //取得相关用户材料数据
            $user_material_where['user_id'] = $loan_apply_info['user_id'];
            $user_material_where['material_id'] = array("in",array_column($loan_apply_material_link_list,"material_id"));
            $user_material_list = IDb::getInstance('user_material')->setDbWhere($user_material_where)->sel();
            if(empty($user_material_list)){
                error("贷款产品申请信息失败！");
            }
        }

        //设置layout
        $this->layout("layout_empty");

        //页面赋值
        $this->assign("loan_apply_id",$loan_apply_id);
        $this->assign("user_material_list",$user_material_list);
        $this->assign("loan_apply_info",$loan_apply_info);

        //渲染模板输出
        return view();
    }

    /**
     * @功能：贷款产品申请日志
     * @开发者：cxl
     * @return string
     */
    public function loan_apply_log(){
        //接收参数
        $loan_apply_id = IRequest::get("loan_apply_id",IRequest::NOT_EMPTY,"贷款产品编号不能为空！");

        //取得日志
        $where['loan_apply_id'] = $loan_apply_id;
        $loan_apply_log_list = IDb::getInstance('loan_apply_log')->setDbWhere($where)->setDbOrder("loan_apply_log_id desc")->pag();

        //设置layout
        $this->layout("layout_empty");

        //页面赋值
        $this->assign("loan_apply_log_list",$loan_apply_log_list);

        //渲染模板输出
        return view();
    }

    /**
     * @功能：小贷产品申请日志
     * @开发者：cxl
     * @return string
     */
    public function small_apply_list(){
        //接收参数
        $user_id = IRequest::get("user_id");
        $small_product_id = IRequest::get("small_product_id");
        $small_apply_status = IRequest::get("small_apply_status");
        $area = IRequest::get("area");

        //整理区域数据
        $province_id = 0;
        $city_id = 0;
        if(!empty($area) && is_array($area)){
            if(!empty($area[0])) $province_id = $area[0]; //取得省份
            if(!empty($area[1])) $city_id = $area[1]; //取得城市
        }

        //取得属性分类列表
        $area_list = IAreaCache::getAllList(2);

        //取得数据
        $where = null;
        if(!empty($user_id)) $where['user_id'] = $user_id;
        if(!empty($small_product_id)) $where['small_product_id'] = $small_product_id;
        if(!empty($small_apply_status)) $where['small_apply_status'] = $small_apply_status;
        if(!empty($province_id)) $where['province_id'] = $province_id;
        if(!empty($city_id)) $where['city_id'] = $city_id;
        $small_apply_list = IDb::getInstance("small_apply")
            ->setDbWhere($where)
            ->setDbOrder("small_apply_time desc")
            ->pag();

        //页面赋值
        $this->assign("user_id",$user_id);
        $this->assign("small_product_id",$small_product_id);
        $this->assign("small_apply_status",$small_apply_status);
        $this->assign("area",$area);
        $this->assign("area_list",$area_list);
        $this->assign("small_apply_list",$small_apply_list);

        //渲染模板输出
        return view();
    }

    /**
     * @功能：小贷产品申请详情
     * @开发者：cxl
     * @return string
     */
    public function small_apply_info(){
        //接收参数
        $small_apply_id = IRequest::get("small_apply_id",IRequest::NOT_EMPTY,"小贷产品申请编号不能为空！");

        //取得日志
        $small_apply_where['small_apply_id'] = $small_apply_id;
        $small_apply_info = IDb::getInstance('small_apply')->setDbWhere($small_apply_where)->row();
        if(empty($small_apply_info)){
            error("小贷产品申请信息失败！");
        }

        //TODO 取得日志
        /*$small_apply_material_link_where['small_apply_id'] = $small_apply_id;
        $small_apply_material_link_list = IDb::getInstance('small_apply_material_link')->setDbWhere($small_apply_material_link_where)->sel();*/

        //取得申请资料信息
        $user_material_list = array();
        if(!empty($small_apply_material_link_list) && is_array($small_apply_material_link_list)){
            //取得相关用户材料数据
            $user_material_where['user_id'] = $small_apply_info['user_id'];
            $user_material_where['material_id'] = array("in",array_column($small_apply_material_link_list,"material_id"));
            $user_material_list = IDb::getInstance('user_material')->setDbWhere($user_material_where)->sel();
            if(empty($user_material_list)){
                error("小贷产品申请信息失败！");
            }
        }

        //设置layout
        $this->layout("layout_empty");

        //页面赋值
        $this->assign("small_apply_id",$small_apply_id);
        $this->assign("user_material_list",$user_material_list);
        $this->assign("small_apply_info",$small_apply_info);

        //渲染模板输出
        return view();
    }

    /**
     * @功能：小贷产品申请日志
     * @开发者：cxl
     * @return string
     */
    public function small_apply_log(){
        //接收参数
        $small_apply_id = IRequest::get("small_apply_id",IRequest::NOT_EMPTY,"小贷产品编号不能为空！");

        //取得日志
        $where['small_apply_id'] = $small_apply_id;
        $small_apply_log_list = IDb::getInstance('small_apply_log')->setDbWhere($where)->setDbOrder("small_apply_log_id desc")->pag();

        //设置layout
        $this->layout("layout_empty");

        //页面赋值
        $this->assign("small_apply_log_list",$small_apply_log_list);

        //渲染模板输出
        return view();
    }
}
